//
//  ZZPayTypeView.m
//  QQplayground
//
//  Created by zhangxy on 2018/8/16.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZPayTypeView.h"

#import "MyButton.h"

#import "UIView+Border.h"

#import "ZTextView.h"
#import "ZZPayHandler.h"

#define CHeight 264

#define CWidth SCREEN_WIDTH - 40


@interface ZZPayTypeView()<UITextViewDelegate>{
    MyButton *checkBtn;
    CGFloat _keyboardHeight;
    NSArray *arr;
    
    
}

@property(nonatomic,strong) UIViewController *curVC;

@property(nonatomic,strong) UIView *bottomView;
@property(nonatomic,strong) UIScrollView *itemScroll;
@property(nonatomic,strong) UILabel *labPrice;


@property (nonatomic, copy) void(^VipPayResult)(int code,NSDictionary *message);
@end


@implementation ZZPayTypeView

-(instancetype)initWithShareType:(UIViewController *)vc result:(void (^)(int, NSDictionary *))payResult{
    self = [super initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self)
    {
        _VipPayResult = payResult;
        _curVC = vc;
        self.backgroundColor = [UIColor clearColor];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmisMenu)];
        [self addGestureRecognizer:tap];
        
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(20, SCREEN_HEIGHT-CHeight, CWidth, CHeight)];
        _bottomView.backgroundColor = UIColor.whiteColor;
        _bottomView.layer.cornerRadius = 5.0f;
        _bottomView.layer.masksToBounds = YES;
        [self addSubview:_bottomView];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmisMenu)];
        [_bottomView addGestureRecognizer:tap1];
        
        [self createInitView];
        
    }
    
    return self;
}
-(void)createInitView{
    UILabel *labTitle = [UILabel new];
    [labTitle setFrame:CGRectMake(15, 17, CWidth - 30, 21)];
    [labTitle setFont:FontFiftent];
    [labTitle setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [labTitle setText:@"选择支付方式"];
    [_bottomView addSubview:labTitle];
    
    _labPrice = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, CWidth - 30, 24)];
    [_labPrice setFont:FontThirteen];
    [_labPrice setTextAlignment:NSTextAlignmentCenter];
    [_labPrice setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [_bottomView addSubview:_labPrice];
    
    
    [self createShareButton:1];
    [self createShareButton:2];
    
    [self createButton];
    
    [self createCloseButton];
    
    
}

-(void)createButton{
    UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnClose setFrame:CGRectMake(15, 210, CWidth - 30, 44)];
    [btnClose setTitle:@"确定" forState:0];
    [btnClose addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [btnClose setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    [btnClose setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
    btnClose.layer.masksToBounds = YES;
    btnClose.layer.cornerRadius = 22.0f;
    [_bottomView addSubview:btnClose];
}


-(void)createCloseButton{
    UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnClose setFrame:CGRectMake(CWidth - 30, 0, 30, 30)];
    [btnClose addTarget:self action:@selector(dissmisMenu) forControlEvents:UIControlEventTouchUpInside];
    [btnClose setBackgroundColor:UIColor.clearColor];
    [btnClose setImage:[UIImage imageNamed:@"发布 关闭 素材"] forState:0];
    btnClose.layer.masksToBounds = YES;
    btnClose.layer.cornerRadius = 22.0f;
    [_bottomView addSubview:btnClose];
}

-(void)createShareButton:(int) i{
    CGFloat y = 99 + 33*(i-1) + 20*(i-1);
    
    NSArray *icons = @[@"微信 分享",@"qq 分享"];
    NSArray *titles = @[@"微信钱包",@"QQ钱包"];
    
    UIImageView *imgAvatar = [[UIImageView  alloc] initWithFrame:CGRectMake(25, y, 33, 33)];
    [imgAvatar setBackgroundColor:UIColor.whiteColor];
    imgAvatar.layer.cornerRadius = 16.5f;
    imgAvatar.layer.masksToBounds = YES;
    [imgAvatar setContentMode:UIViewContentModeScaleAspectFit];
    [imgAvatar setImage:[UIImage imageNamed:icons[i-1]]];
    [_bottomView addSubview:imgAvatar];
    
    
    UILabel *labName = [[UILabel alloc] initWithFrame:CGRectMake(77, y, CWidth - 77 - 60, 33)];
    [labName setFont:FontSeventeen];
    [labName setText:titles[i-1]];
    [labName setTextColor:UIColorFromRGB(0x262626)];
    [_bottomView addSubview:labName];
    
    
    MyButton *btn = [MyButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(CWidth - 25 - 43, y, 43, 33)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(10, 0, 5, 10)];
    [btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [btn setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:UIControlStateNormal];
    [btn.titleLabel setFont:FontFiftent];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(shareWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"ic_check_unselect"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"ic_check_select"] forState:UIControlStateSelected];
    btn.tag = i;
    
    if(i == 1){
        btn.selected = YES;
        checkBtn = btn;
    }else{
        btn.enabled = NO;
    }
}


-(void)shareWithButton:(MyButton *) shareButton{
    if(checkBtn){
        checkBtn.selected = NO;
    }
    shareButton.selected = YES;
    checkBtn = shareButton;
}


-(void)actionButton:(UIButton *) btn{
    NSDictionary *dict = @{@"session_uid":[UserUtil getUser].user_id,@"goods_sn":_pageInfo.goods_sn,@"attr_id":_pageInfo.checkType.attr_id};
    
    [ZZPayHandler startJumppay:ZZPayTypeWX order:dict result:^(int code, NSDictionary *msg) {
        if(code == 0){
            [self dissmisMenu];
        }
        if(_VipPayResult){
            _VipPayResult(code,msg);
        }
    }];
}


- (void)dissmisMenu{
    CGRect sheetViewF = self.frame;
    sheetViewF.origin.y = SCREEN_HEIGHT;
    self.backgroundColor = UIColor.clearColor;
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.alpha = 0.0;
        self.frame = sheetViewF;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)show{
    
    
    NSString *text = [NSString stringWithFormat:@"支付 %@ 元",_pageInfo.checkType.price];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text];
    [string addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(BgTitleColor) range:NSMakeRange(2, text.length - 3)];
    [_labPrice setAttributedText:string];
    CGRect sheetViewF = self.frame;
    sheetViewF.origin.y = 0;
    self.frame = sheetViewF;
    
    CGRect bf = _bottomView.frame;
    bf.origin.y = ScreenHeight/2 - CHeight/2;
    [UIView animateWithDuration:0.3 animations:^{
        self->_bottomView.frame = bf;
        self.backgroundColor = UIColorFromRGBAlpha(TextBlackColor, 0.4);
    } completion:^(BOOL finished) {
        
    }];
}

@end
